home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / PredatorPrey / FlightSim / flight.c next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  5.1 KB  |  322 lines  |  [TEXT/KAHL]

  1. #include "flight.h"
  2.  
  3.  
  4.  
  5. WindowPtr        theWind;    /*the main window*/
  6. WindowRecord    windRecord;    /*storage for the window*/
  7.  
  8.  
  9. GrafPtr            windManPort;    /*window manager port*/
  10. EventRecord     theEvent;    /*the main event*/
  11.  
  12.  
  13. MenuHandle        appleMenu, fileMenu, editMenu;
  14.  
  15.  
  16. main()
  17. {
  18.     Init();
  19.     MakeMenus();
  20.     MakeWindow();
  21.     AddrsSet();        /*    fxGrid.c    */
  22.  
  23.     MakeGrid();        /*    fxGrid.c    */
  24.     DebugStr("\PHello");
  25.  
  26.     while (1)
  27.     {
  28.         SystemTask();
  29.         if (GetNextEvent(everyEvent, &theEvent))
  30.             DoEvent();
  31.     }
  32. }
  33.  
  34. DoEvent()
  35. {
  36.     switch (theEvent.what) {
  37.     case mouseDown:
  38.         DoMouseDown();
  39.         break;
  40.     case keyDown:
  41.     case autoKey:
  42.         DoKeyDown();
  43.         break;
  44.     case updateEvt:
  45.         DoUpdate();
  46.         break;
  47.     case diskEvt:
  48.         break;
  49.     case activateEvt:
  50.         DoActivate();
  51.         break;
  52.     }
  53. }
  54.  
  55. DoMouseDown()
  56. {
  57.     int thePart;
  58.     WindowPtr whichWindow;
  59.  
  60.     thePart = FindWindow( theEvent.where, &whichWindow);
  61.  
  62.     switch (thePart) {
  63.     case inDesk:
  64.         break;
  65.     case inMenuBar:
  66.         DoMenuClick();
  67.         break;
  68.     case inSysWindow:
  69.         SystemClick(&theEvent, whichWindow);
  70.         break;
  71.     case inContent:
  72.         DoContent(whichWindow);
  73.         break;
  74.     case inDrag:
  75.         DoDrag(whichWindow);
  76.         break;
  77.     case inGrow:
  78.         DoGrow(whichWindow);
  79.         break;
  80.     case inGoAway:
  81.         if (TrackGoAway(whichWindow, theEvent.where))
  82.             DoQuit();
  83.         break;
  84.     case inZoomIn:
  85.     case inZoomOut:
  86.         DoZoom(whichWindow, thePart);
  87.         break;
  88.     }
  89. }
  90.  
  91. DoMenuClick()
  92. {
  93.     long menuChoice;
  94.  
  95.     menuChoice = MenuSelect(theEvent.where);
  96.     DoMenuChoice(menuChoice);
  97. }
  98.  
  99. DoMenuChoice(menuChoice)
  100. long menuChoice;
  101. {
  102.     switch(HiWord(menuChoice)) {
  103.     case MENU_APPLE:
  104.         DoAppleChoice( LoWord(menuChoice) );
  105.         break;
  106.     case MENU_FILE:
  107.         DoFileMenu(LoWord(menuChoice));
  108.         break;
  109.     case MENU_EDIT:
  110.         DoEditMenu(LoWord(menuChoice));
  111.         break;
  112.     }
  113.  
  114.     HiliteMenu(0); /* unhilight the selected menu */
  115. }
  116.  
  117. DoAppleChoice(theItem)
  118. int theItem;
  119. {
  120.     Str255    accName;
  121. /*    char    accName[256];*/
  122.     int        accNum, i;
  123.     
  124.     if (theItem EQ 1)    
  125.         Alert(ALRT_ABOUT, NIL);
  126.     else
  127.     {
  128.         GetItem(appleMenu, theItem, /*(Str255)*/accName);
  129.         accNum = OpenDeskAcc(accName);
  130.     }
  131. }
  132.  
  133. DoFileMenu(theItem)
  134. int theItem;
  135. {
  136.     if (theItem EQ 1)
  137.         DoQuit();
  138. }
  139.  
  140. DoQuit()
  141. {
  142.     ExitToShell();
  143. }
  144.  
  145. DoEditMenu(theItem)
  146. int theItem;
  147. {
  148.     switch(theItem) {
  149.     case 1: /* Undo */
  150.         SystemEdit(undoCmd);
  151.         break;
  152.     case 3: /* Cut */
  153.         SystemEdit(cutCmd);
  154.         break;
  155.     case 4: /* Copy */
  156.         SystemEdit(copyCmd);
  157.         break;
  158.     case 5: /* Paste */
  159.         SystemEdit(pasteCmd);
  160.         break;
  161.     case 6: /* Clear */
  162.         SystemEdit(clearCmd);
  163.         break;
  164.     }
  165. }
  166.  
  167. DoContent(whichWindow)
  168. WindowPtr whichWindow;
  169. {
  170.     Point    localPt;
  171.  
  172.     if (whichWindow NEQ FrontWindow())
  173.         SelectWindow(whichWindow);
  174.     else
  175.     {
  176.         localPt = theEvent.where;
  177.         GlobalToLocal(&localPt);
  178.         DoMouseMove(&localPt);
  179.     }
  180. }
  181.  
  182. DoDrag(whichWindow)
  183. WindowPtr whichWindow;
  184. {
  185.     Rect limitRect;
  186.  
  187.     if (whichWindow NEQ FrontWindow())
  188.         SelectWindow(whichWindow);
  189.     else
  190.     {
  191.         SetRect(&limitRect, 0, MENU_BAR_HEIGHT, 5000, 5000);
  192.         DragWindow(whichWindow, theEvent.where, &limitRect);
  193.     }
  194. }
  195.  
  196. DoGrow(whichWindow)
  197. WindowPtr whichWindow;
  198. {
  199.     Rect limitRect;
  200.     long    newSize;
  201.  
  202. return;
  203.     SetRect(&limitRect, 50, 50, 2000, 2000);
  204.     newSize = GrowWindow(whichWindow, theEvent.where, &limitRect);
  205.     if (newSize)
  206.     {
  207.         EraseRect(&whichWindow->portRect);
  208.         SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), TRUE);
  209.     }
  210. }
  211.  
  212. DoKeyDown()
  213. {
  214.     char    ch;
  215.     long    menuChoice;
  216.     
  217.     ch = (char)(theEvent.message & charCodeMask);
  218.     
  219.     if ((theEvent.modifiers & cmdKey)
  220.         AND (theEvent.what NEQ autoKey)) /* ignore repeats */
  221.     {
  222.         menuChoice = MenuKey(ch);
  223.         if (menuChoice)
  224.             DoMenuChoice(menuChoice);
  225.     }
  226. }
  227.  
  228. DoUpdate()
  229. {
  230.     GrafPtr savePort;        /* to save and restore the old port */
  231.     WindowPtr whichWindow;
  232.  
  233.     whichWindow = (WindowPtr) (theEvent.message);
  234.  
  235.     BeginUpdate(whichWindow);    /* reset ClipRgn etc to only redraw what's
  236.                                necessary. */
  237.  
  238.         GetPort(&savePort);        /* don't trash the port; we might be
  239.                                    updating an inactive window */
  240.         SetPort(whichWindow);    /* work in the specified window */
  241.  
  242.         if (whichWindow EQ theWind)
  243.         {
  244.             EraseRect(&(whichWindow->portRect));
  245.             DoForward();
  246.         }
  247.     EndUpdate(whichWindow);
  248.  
  249.     SetPort(savePort);
  250. }
  251.  
  252. DoActivate()
  253. {
  254.     WindowPtr whichWindow;
  255.  
  256.     whichWindow = (WindowPtr) (theEvent.message);
  257.     if (whichWindow EQ theWind)
  258.         /*DrawGrowIcon(whichWindow);*/
  259.  
  260.     if (theEvent.modifiers & activeFlag)
  261.         SetPort(whichWindow);
  262. }
  263.  
  264. DoZoom(whichWindow, thePart)
  265. WindowPtr    whichWindow;
  266. int            thePart;
  267. {
  268. return;
  269.  
  270.     if (TrackBox(whichWindow, theEvent.where, thePart))
  271.         ZoomWindow(whichWindow, thePart, TRUE);
  272. }
  273.  
  274.  
  275.  
  276. Init()
  277. {
  278.     pascal void restartProc();
  279.  
  280.     InitGraf(&thePort);
  281.     InitFonts();
  282.     InitWindows();
  283.     InitMenus();
  284.     TEInit();
  285.     InitDialogs(restartProc);        
  286.     InitCursor();
  287.     
  288.     FlushEvents(everyEvent, 0);
  289.  
  290.     GetWMgrPort(&windManPort);    /* get whole screen port that window mgr
  291.                    uses */
  292.     SetPort(windManPort);    /* and start off with it */
  293.  
  294.     SetEventMask(everyEvent);
  295. }
  296.  
  297. /* when the world comes to an end */
  298. pascal void
  299. restartProc()
  300. {
  301.     ExitToShell();
  302. }
  303.  
  304. MakeMenus()
  305. {
  306.     appleMenu = GetMenu(MENU_APPLE);
  307.     AddResMenu(appleMenu, 'DRVR');
  308.     InsertMenu(appleMenu, 0);
  309.     
  310.     fileMenu = GetMenu(MENU_FILE);
  311.     InsertMenu(fileMenu, 0);
  312.     
  313.     editMenu = GetMenu(MENU_EDIT);
  314.     InsertMenu(editMenu, 0);
  315.     
  316.     DrawMenuBar();
  317. }
  318.  
  319. MakeWindow()
  320. {
  321.     theWind = GetNewWindow(WIND_MAIN, &windRecord, (WindowPtr)-1L);    
  322. }